var flag;
/**
 * 初始化
 */
$(function(){
	getAllAddress();
	
	// 设置是否默认
	$("#ifDefault").on("click",function(){
		$("#checkIpt").toggle();
		$("#checkedIpt").toggle();
	});
	
	$("#saveAddressBtn").on("click",function(){
		saveAddress();
	});
	
	$("#cancelBtn").on("click",function(){
		$("#myModal").modal('hide');
	});
	
	$('#myModal').on('hidden.bs.modal', function () {
		$("#addAddressForm")[0].reset();
	});
	
});


/**
 * 获取当前登录用户的所有收货地址
 */
function getAllAddress(){
	$.ajax({
		type:"get",
		url:basePath + "/address/list",
		async:true,
		success:function(data){
			if(data){
				if(data.obj == null || data.obj == ""){
					$(".emptyDiv").show();
					$(".existAddress").hide();
					$(".addressList").hide();
				}else{
					loadData(data.obj);
					
					$(".emptyDiv").hide();
					$(".existAddress").show();
					$(".addressList").show();
				}
			}
		}
	});
}


/**
 * 新增地址
 */
function addAddress(){
	flag = true;
	$("#myModalLabel").html("新建地址");
	fullArea();
}

/**
 * 修改地址
 */
function modifyAddress(obj){
	flag = false;
	fullArea();
	$("#myModalLabel").html("修改地址");
	var addressId = $(obj).parent().siblings("td.addressId").html();
	
	$.ajax({
		type:"get",
		url:basePath + "/address/detail/"+addressId,
		async:true,
		success:function(data){
			if(data){
				if(data.status == "200"){
					var area = data.obj.streetName.split(",");
					$("#cityName").val(area[0]);
					$("#areaName").val(area[1]);
					$("#subAreaName").val(area[2]);
					$("#streetNameTextArea").val(area[3]);
					
					var input = $("#addAddressForm input");
					for(var i in input){
						for(var j in data.obj){
							if(input.eq(i).attr("name") == j){
								input.eq(i).val(data.obj[j]);
							}
						}
					}
					
					// 是否默认
					if(data.obj.isDefault == 1){
						$("#checkIpt").css("display","none");
						$("#checkedIpt").css("display","block");
					}else{
						$("#checkIpt").css("display","block");
						$("#checkedIpt").css("display","none");
					}
					
					$("#myModal").modal('show');
				}
				
				
			}
		}
	});
}

/**
 * 保存地址
 */
function saveAddress(){
	var adeData = $("#addAddressForm").serializeObject();
	
	if($("#checkIpt").css("display") == "none"){
		adeData.isDefault = 1;
	}else{
		adeData.isDefault = 0;
	}
	
	adeData.streetName = $("#cityName").val() + "," + $("#areaName").val() + "," + $("#subAreaName").val() + "," + adeData.streetName ;
	
	
	if(flag){
		var url = basePath + "/address/add";
	}else{
		var url = basePath + "/address/edit";
	}
	$.ajax({
		type:"post",
		url:url,
		data:adeData,
		async:true,
		success:function(data){
			if(data){
				if(data.status == "200"){
					getAllAddress();
					$("#myModal").modal('hide');
				}
			}
		}
	});
}

/**
 * 加载数据
 */
function loadData(dataList){
	
	var $tbody = $("#addressTable tbody");
	$tbody.html("");
	
	for(var i in dataList){
		var $tr = $("<tr><td style='display:none;' class='addressId'>"+dataList[i].addressId+"</td><td>"+dataList[i].userName+"</td><td>"+dataList[i].streetName+"</td><td>"+dataList[i].tel+"</td>" +
				"<td><a href='javascript:;' onclick='modifyAddress(this)'>修改</a>&nbsp;&nbsp;<a href='javascript:;' onclick='removeAddress(this)'>删除</a>" +
						"</td><td><span style='display:"+(dataList[i].isDefault ?'inline-block':'none')+"'>默认地址</span></td></tr>");
		$tbody.append($tr);
	}
}

/**
 * 删除地址
 */
function removeAddress(obj){
	var addressId = $(obj).parent().siblings("td.addressId").html();
	$("#removeModal").modal("show");
	
	$("#sureRemove").on("click",function(){
		$.ajax({
			type:"post",
			url:basePath + "/address/del/" + addressId ,
			async:true,
			success:function(data){
				if(data){
					
					if(data.status == "200"){
						getAllAddress();
					}
				}
			}
		});
	});
	
}

/**
 * 地址加载
 */
function getArea(level){
	var datalist = [];
	$.ajax({
		type:"get",
		url:basePath + "/address/area/" + level,
		async:false,
		success:function(data){
			if(data){
				datalist = data.obj;
			}
			
		}
	});
	return datalist;
}

/**
 * 一级地址数据填充
 */
function fullArea(){
	var cityData = getArea(1);
	var $city = $("#cityName");
	$city.html("");
	 $("#areaName").html("");
	 $("#subAreaName").html("");
	var allOption = $("<option value=''  data-id=''>全部</option>");
	$city.append(allOption);
	
	for(var i in cityData){
		var $option = $("<option value='"+cityData[i].name+"'  data-id='"+cityData[i].id+"'>"+cityData[i].name+"</option>");
		 $city.append($option);
	}
}

/**
 * 二级地址填充
 */
function areaData(obj){
	var pid = $(obj).find("option:selected").attr("data-id");
	var areaData = getArea(pid);
	var $area = $("#areaName");
	$area.html("");
	$("#subAreaName").html("");
	var allOption = $("<option value=''  data-id=''>全部</option>");
	$area.append(allOption);
	
	for(var i in areaData){
		var $option = $("<option value='"+areaData[i].name+"'  data-id='"+areaData[i].id+"'>"+areaData[i].name+"</option>");
		$area.append($option);
	}
}

/**
 * 三级地址填充
 */
function subareaData(obj){
	var pid = $(obj).find("option:selected").attr("data-id");
	var subareaData = getArea(pid);
	var $subarea = $("#subAreaName");
	$subarea.html("");
	var allOption = $("<option value=''  data-id=''>全部</option>");
	$subarea.append(allOption);
	
	for(var i in subareaData){
		var $option = $("<option value='"+subareaData[i].name+"'  data-id='"+subareaData[i].id+"'>"+subareaData[i].name+"</option>");
		$subarea.append($option);
	}
}


